package com.talent.jiwen.training;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;

import com.talent.jiwen.R;
import com.talent.jiwen.base.BaseActivity;
import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.http.result.tiku.SectionBean;
import com.talent.jiwen.training.data.IdNameBean;
import com.talent.jiwen.util.GsonUtil;
import com.talent.jiwen.util.Validators;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.Observable;

public class SelectChapterActivity extends BaseActivity {
    @BindView(R.id.expandableListView)
    ExpandableListView expandableListView;
    @BindView(R.id.sureBtn)
    Button sureBtn;

    private String gradeId = "";
    private IdNameBean selectItem  = null;

    private SelectChapterAdapter selectChapterAdapter;
    private List<SectionBean.ChildBeanX> dataList = new ArrayList<>();

    public static final String PARAM_GRADE_ID = "param.grade.id";
    public static final String PARAM_SELECT_ITEM  = "param.select.item";

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_select_chapter;
    }

    @Override
    public String titleName() {
        return null;
    }

    @Override
    protected void initData() {
        gradeId = getIntent().getStringExtra(PARAM_GRADE_ID);
        String selectJsonStr = getIntent().getStringExtra(PARAM_SELECT_ITEM);
        selectItem = GsonUtil.getInstance().json2Bean(selectJsonStr , IdNameBean.class);

        getChapterList();

        sureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IdNameBean result = selectChapterAdapter.getChapterSelectItem();
                if (Validators.isEmpty(result.getIdStr())){
                    showToast("请选择章节");
                }else{
                    EventBus.getDefault().post(result);
                    finish();
                }

            }
        });
    }


    private void getChapterList(){

        Observable observable = ApiFactory.getTiKuApiSingleton().getSection(gradeId);

        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<SectionBean>(mContext) {
                    @Override
                    protected void _onNext(SectionBean sectionBean) {
                        dataList.clear();
                        dataList.addAll(sectionBean.getChild());

                        selectChapterAdapter = new SelectChapterAdapter(SelectChapterActivity.this , dataList , selectItem);
                        expandableListView.setAdapter(selectChapterAdapter);
                    }

                    @Override
                    protected void _onError(String message) {
                        showToast(message);
                    }
                }
                , ActivityLifeCycleEvent.DESTROY, lifecycleSubject, true);
    }

    /**启动该页面
     *
     * @param context
     * @param gradeId
     * @param idNameBean
     */
    public static void startSelectChapterActivity(Context context , String gradeId , IdNameBean idNameBean){
        Intent intent = new Intent();
        intent.setClass(context , SelectChapterActivity.class);
        intent.putExtra(PARAM_GRADE_ID , gradeId);
        intent.putExtra(PARAM_SELECT_ITEM , GsonUtil.getInstance().toJson(idNameBean));
        context.startActivity(intent);
    }
}
